/*
 * @Description: 
 * @version: 
 * @Author: wyx
 * @Date: 2020-09-22 14:16:31
 * @LastEditDate: Do not exit
 */
import axios from 'axios'
import SharedData  from '@/utils/localStorage.js'

// 创建axios实例
const service = axios.create({
    baseURL: 'http://localhost:8008', // api的base_url
    // baseURL:'http://localhost',
    timeout: 5000 // 请求超时时间
  })

  // request拦截器
  service.interceptors.request.use(config => {
    console.log("请求拦截"+config.url);
    // 不是登录就添加令牌token
    if(config.url.indexOf("login")==-1){
      config.headers["token"]=SharedData.getToken();
    }
    return config
  }, error => {
    return Promise.reject(error);
  })
  
  // respone拦截器
  service.interceptors.response.use(
    response => {
      console.log("响应拦截");
      if(response.data.code==="1111"){
        // clearToken();
        SharedData.clearToken();
        window.location.href="/login";
      }
      return response;
    }, error => {
      // return Promise.reject(error.response.data)
      return Promise.reject(error);
    })
  
  export default service
